#pragma once
#include"reserve_lterator.h"
#include<assert.h>

namespace study
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		typedef ReverseIterator< iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator< const_iterator,const T&,const T*> const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return reverse_iterator(end() - 1);
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin() - 1);
		}

		const_reverse_iterator rbegin()
		{
			return const_reverse_iterator(end() - 1);
		}

		const_reverse_iterator rend()
		{
			return const_reverse_iterator(begin() - 1);
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}

		const_iterator end()const
		{
			return _finish;
		}

		vertor(){}

		template<class InputIterator>
		vector(InputIterator first, InputIterator second)
		{
			while (first != second)
			{
				push_back(*first);
				++first;
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			reserve(n);  
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(const T& v = T())
		{
			reserve(v.capcacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		vector<T>& operator=(vector<T> tmp)
		{
			swap(tmp);
			return *this;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t sz = size();

				if (_start)
				{
					for (int i = 0; i < sz; i++)
					{
						tmp[i]=_start[i]
					}
					delete _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		void resize(size_t n, const T* val)
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					finish++;
				}
			}
		}

		size_t size()const
		{
			return _finish - _start;
		}

		size_t capacity()const
		{
			return __endofstorage - _start;
		}

		T* operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T* operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void insert(size_t pos, const & x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			_finish++;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			--_finish;
			return pos;
		}

	private:
		iterator _start=nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	
	};

	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		for (size_t i = 0; i < v.size(); i++)
		{
			cout << v[i] << " ";
		}
		cout << endl;

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			*it *= 10;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

};

